﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shopping_cart.Models;

namespace Shopping_cart.Controllers
{
    public class HomeController : Controller
    {
        public List<Product> productList = new List<Product>();
        private NewProductContext db = new NewProductContext();

        public HomeController()
        {
            productList = db.Products.ToList();
        }

        public ActionResult Index()
        {
            return View(productList);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult PrintReceipt(int[] selected, Product[] product)
        {
           List<Product> returnedProductList = new List<Product>();

            foreach (var srt in selected)
            {
                Product p = productList.SingleOrDefault(prd => prd.Id == srt);
                var singleOrDefault = product.SingleOrDefault(prd => prd.Id == srt);
                if (singleOrDefault != null)
                    p.Price = singleOrDefault.Count*p.Price;
                returnedProductList.Add(p);
            }

            return View(returnedProductList);
        }

        public decimal GetDiscount(int numberOfItems)
        {
            if (numberOfItems <= 2)
            {
                return (decimal)0.05;
            }
            if (numberOfItems > 2 && numberOfItems <= 4)
            {
                return (decimal)0.1;
            }
            return (decimal)0.15;
        }

        public ActionResult Payment()
        {
            return View();
        }

        public ActionResult ComfirmPayment()
        {
            return View();
        }
    }
}
